﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.DelegateDecl"/>.
    /// </summary>
    public class DelegateDeclVM : TypeDeclVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(DelegateDecl),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new DelegateDeclVM((DelegateDecl)codeObject, isDescription, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected ExpressionVM _returnTypeVM;
        protected ChildListVM<ParameterDeclVM> _parameterVMs;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.DelegateDecl"/>.
        /// </summary>
        public DelegateDeclVM(DelegateDecl delegateDecl, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(delegateDecl, isDescription, dictionary)
        {
            _returnTypeVM = (ExpressionVM)CreateVM(delegateDecl.ReturnType, isDescription, dictionary);
            _parameterVMs = CreateListVM<ParameterDecl, ParameterDeclVM>(delegateDecl.Parameters, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.DelegateDecl"/> model.
        /// </summary>
        public DelegateDecl DelegateDecl
        {
            get { return (DelegateDecl)CodeObject; }
        }

        /// <summary>
        /// The return type of the delegate (never null - will be type 'void' instead).
        /// </summary>
        public ExpressionVM ReturnTypeVM
        {
            get { return (_returnTypeVM ?? (TypeRefVM)CreateVM(TypeRef.VoidRef)); }
        }

        /// <summary>
        /// The list of <see cref="ParameterDeclVM"/>s.
        /// </summary>
        public ChildListVM<ParameterDeclVM> ParameterVMs
        {
            get { return _parameterVMs; }
        }

        /// <summary>
        /// True if there are any parameters.
        /// </summary>
        public bool HasParameterVMs
        {
            get { return (_parameterVMs != null && _parameterVMs.Count > 0); }
        }

        /// <summary>
        /// The number of parameters.
        /// </summary>
        public int ParameterVMCount
        {
            get { return (_parameterVMs != null ? _parameterVMs.Count : 0); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public static readonly Brush StaticBorderBrush = DarkLavender;
        public static readonly Brush StaticBackgroundBrush = Brushes.Lavender;

        public override Brush BorderBrush
        {
            get { return StaticBorderBrush; }
        }

        public override Brush BackgroundBrush
        {
            get { return StaticBackgroundBrush; }
        }

        protected override bool RenderParens()
        {
            return !MethodDeclBaseVM.HideMethodParens;
        }

        protected override void RenderStatement(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            renderer.RenderText(DelegateDecl.ParseToken, KEYWORD_BRUSH, this);
            _returnTypeVM.Render(renderer, passFlags | RenderFlags.IsPrefix | RenderFlags.PrefixSpace);
            base.RenderArgument(renderer, flags);
        }

        protected override void RenderArgumentPrefix(CodeRenderer renderer, RenderFlags flags)
        {
            if (MethodDeclBaseVM.HideMethodParens && ((_parameterVMs != null && _parameterVMs.Count > 0) || DelegateDecl.HasInfixComments))
                base.RenderArgumentPrefix(renderer, flags);
        }

        protected override void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            RenderInfixComments(renderer, 0, flags);
            renderer.RenderList(_parameterVMs, passFlags, this);
            if (DelegateDecl.IsEndFirstOnLine)
                renderer.NewLine();
        }

        public override void UnRender()
        {
            _returnTypeVM.UnRender();
            ChildListHelpers.UnRender(_parameterVMs);
            base.UnRender();
        }

        /// <summary>
        /// Get the <see cref="FrameworkElement"/> to be highlighted when the object is selected.
        /// </summary>
        public override FrameworkElement GetSelectionElement()
        {
            return FrameworkElement;
        }

        #endregion
    }
}
